package com.fudan.gamestore.controller;

import com.fudan.gamestore.pojo.Game;
import com.fudan.gamestore.pojo.Response;
import com.fudan.gamestore.pojo.User;
import com.fudan.gamestore.service.CartService;
import com.fudan.gamestore.service.GameService;
import com.fudan.gamestore.service.TokenService;
import com.fudan.gamestore.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private UserService userService;
    @Autowired
    private GameService gameService;
    @Autowired
    private TokenService tokenService;

    @RequestMapping(value = "/user/cart/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getCartByUserId(@PathVariable("userId") Integer userId, @RequestHeader String token) {
        Map<String, Object> retMap = new HashMap<>();
        if(tokenService.checkToken(token, userId, 0) < 0){
            retMap.put("status", 400);
            return retMap;
        }
        retMap.put("cartItemList", cartService.getCartByUserId(userId));
        retMap.put("status", 200);
        return retMap;
    }

    @RequestMapping(value = "/cart/add", method = RequestMethod.POST)
    @ResponseBody
    public Response addGameToCart(@RequestBody Map<String, Object> map, @RequestHeader String token) {
        Integer gameId = (Integer) map.get("gameId");
        Integer userId = (Integer) map.get("userId");

        if(tokenService.checkToken(token, userId, 0) < 0){
            return new Response(400, "未授权");
        }

        //判断用户是否存在
        User user = userService.getUserById(userId);
        if (user == null) {
            return new Response(400, "user not found");
        }
        //判断是否已拥有
        List<Game> games;
        if ((games = gameService.getOwnGameByUserId(userId)) != null) {
            for (Game game : games) {
                if (gameId.equals(game.getGameId())) {
                    return new Response(400, "用户已拥有");
                }
            }
        }

        if (cartService.checkIfInCart(userId, gameId) == 1) {
            return new Response(400, "game is already in cart");
        } else {
            if (cartService.addGameToCart(userId, gameId) == 1)
                return new Response(200, "success");
            else
                return new Response(400, "failed to add game to cart");
        }
    }

    @RequestMapping(value = "/cart/delete/all", method = RequestMethod.POST)
    @ResponseBody
    public Response clearCart(@RequestBody Map<String, Object> map, @RequestHeader String token) {
        Integer userId = (Integer) map.get("userId");
        if(tokenService.checkToken(token, userId, 0) < 0){
            return new Response(400, "未授权");
        }

        cartService.clearCart(userId);
        return new Response(200, "success");
    }

    @RequestMapping(value = "/cart/delete", method = RequestMethod.POST)
    @ResponseBody
    public Response deleteCartItems(@RequestBody Map<String, Object> map, @RequestHeader String token) {
        Integer userId = (Integer) map.get("userId");
        if(tokenService.checkToken(token, userId, 0) < 0){
            return new Response(400, "未授权");
        }
        List<Integer> cartItemIds = (List<Integer>) map.get("cartItemIds");
        cartService.deleteCartItems(cartItemIds);
        return new Response(200, "success");
    }
}
